package org.kzwl.service.web.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.pojo.bo.menu.MenuBasicBO;
import org.kzwl.comm.pojo.dto.menu.MenuDTO;
import org.kzwl.entity.web.CuMenu;
import org.kzwl.mapper.web.MenuMapper;
import org.kzwl.service.web.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 场馆预约 服务实现类
 * </p>
 *
 * @author 张国伟/郭家恒
 * @since 2020-12-18
 */
@Service
public class CuMenuServiceImpl extends ServiceImpl<MenuMapper, CuMenu> implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    //region 查询menu
    @Override
    public List<MenuDTO> queryBasic(MenuBasicBO bo) {
        List<CuMenu> list = menuMapper.queryBasic(bo.getCuAdminId());
        List<MenuDTO> dtos = new ArrayList<>();
        list.forEach(l->{
            MenuDTO.Meta mata = new MenuDTO.Meta();
            mata.setTitle(l.getTitle());
            mata.setIcon(l.getIcon());
            mata.setHideInBread(l.getHideInBread());
            mata.setNotCache(l.getNotCache());
            MenuDTO dto = new MenuDTO();
            BeanUtils.copyProperties(l,dto);
            dto.setMeta(mata);
            dto.setPath("/"+l.getName());
            dtos.add(dto);
        });
        MenuDTO dto = new MenuDTO();
        dto.setCuMenuId(0);
        foreach(dto, dtos);
        return dto.getChildren();
    }
    //endregion

    private void foreach(MenuDTO main, List<MenuDTO> list) {
        for (MenuDTO menuDTO : list) {
            if (main.getCuMenuId().longValue() == menuDTO.getCuMenuParent().longValue()) {
                foreach(menuDTO, list);
                main.getChildren().add(menuDTO);
                CollectionUtil.sort(main.getChildren(), Comparator.comparingInt(MenuDTO::getCuMenuParent));
            }
        }
    }

}
